home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / nroff1.zip / NRO.C < prev    next >
C/C++ Source or Header  |  1990-12-07  |  6KB  |  251 lines

  1. /*
  2.  *      Word Processor
  3.  *      similar to Unix NROFF or RSX-11M RNO -
  4.  *      adaptation of text processor given in
  5.  *      "Software Tools", Kernighan and Plauger.
  6.  *
  7.  *      Stephen L. Browning
  8.  *      5723 North Parker Avenue
  9.  *      Indianapolis, Indiana 46220
  10.  *
  11.  *    Ported to MS C 5.1 
  12.  *      by John Dudeck (jdudeck@polyslo.calpoly.edu) 11/25/90.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "nro.h"
  17. #define EXTERN
  18. #include "nroxtrn.h"
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.         int i;
  25.         int swflg;
  26.         int ifp, ofp;
  27.         
  28.         swflg = FALSE;
  29.         pout = stdout;
  30.         ifp = ofp = 0;
  31.         init();
  32.         for (i=1; i<argc; ++i) {
  33.                 if (*argv[i] == '-' || *argv[i] == '+') {
  34.                         if (pswitch(argv[i],&swflg) == ERR) exit(-1);
  35.                 }
  36.         }
  37.         for (i=1; i<argc; ++i) {
  38.                 if (*argv[i] != '-' && *argv[i] != '+' && *argv[i] != '>') {
  39.                         if ((sofile[0] = fopen(argv[i],"r")) == NULL) {
  40.                                 printf("nro: unable to open file %s\n",argv[i]);
  41.                                 exit(-1);
  42.                         }
  43.                         else {
  44.                                 profile();
  45.                                 fclose(sofile[0]);
  46.                         }
  47.                 }
  48.         }
  49.         if (argc == 1) {
  50.                 puts("Usage: nro [-n] [+n] [-pxx] [-v] [-u] [-b] [-mmacfile] infile ... [>outfile]\n");
  51.                 exit(-1);
  52.         }
  53. }
  54.  
  55.  
  56.  
  57. /*
  58.  *      retrieve one line of input text
  59.  */
  60.  
  61. getlin(p,in_buf)
  62. char *p;
  63. FILE *in_buf;
  64. {
  65.         int i;
  66.         int c;
  67.         char *q;
  68.  
  69.         q = p;
  70.         for (i=0; i<MAXLINE-1; ++i) {
  71.                 c = ngetc(in_buf);
  72.                 if (c == CPMEOF || c == EOF) {
  73.                         *q = EOS;
  74.                         c = strlen(p);
  75.                         return(c == 0 ? EOF : c);
  76.                 }
  77.                 *q++ = c;
  78.                 if (c == '\n') break;
  79.         }
  80.         *q = EOS;
  81.         return(strlen(p));
  82. }
  83.  
  84.  
  85.  
  86. /*
  87.  *      initialize parameters for nro word processor
  88.  */
  89.  
  90. init()
  91. {
  92.         int i;
  93.  
  94.         dc.fill = YES;
  95.         dc.lsval = 1;
  96.         dc.inval = 0;
  97.         dc.rmval = PAGEWIDTH - 1;
  98.         dc.tival = 0;
  99.         dc.ceval = 0;
  100.         dc.ulval = 0;
  101.         dc.cuval = 0;
  102.         dc.juval = YES;
  103.         dc.boval = 0;
  104.         dc.bsflg = FALSE;
  105.         dc.buflg = FALSE;
  106.         dc.pgchr = '#';
  107.         dc.cmdchr = '.';
  108.         dc.prflg = TRUE;
  109.         dc.sprdir = 0;
  110.         for (i=0; i<26; ++i) dc.nr[i] = 0;
  111.         pg.curpag = 0;
  112.         pg.newpag = 1;
  113.         pg.lineno = 0;
  114.         pg.plval = PAGELEN;
  115.         pg.m1val = 2;
  116.         pg.m2val = 2;
  117.         pg.m3val = 2;
  118.         pg.m4val = 2;
  119.         pg.bottom = pg.plval - pg.m4val - pg.m3val;
  120.         pg.offset = 0;
  121.         pg.frstpg = 0;
  122.         pg.lastpg = 30000;
  123.         pg.ehead[0] = pg.ohead[0] = '\n';
  124.         pg.efoot[0] = pg.ofoot[0] = '\n';
  125.         for (i=1; i<MAXLINE; ++i) {
  126.                 pg.ehead[i] = pg.ohead[i] = EOS;
  127.                 pg.efoot[i] = pg.ofoot[i] = EOS;
  128.         }
  129.         pg.ehlim[LEFT] = pg.ohlim[LEFT] = dc.inval;
  130.         pg.eflim[LEFT] = pg.oflim[LEFT] = dc.inval;
  131.         pg.ehlim[RIGHT] = pg.ohlim[RIGHT] = dc.rmval;
  132.         pg.eflim[RIGHT] = pg.oflim[RIGHT] = dc.rmval;
  133.         co.outp = 0;
  134.         co.outw = 0;
  135.         co.outwds = 0;
  136.         for (i=0; i<MAXLINE; ++i) co.outbuf[i] = EOS;
  137.         for (i=0; i<MXMDEF; ++i) mac.mnames[i] = NULL;
  138.         mac.lastp = 0;
  139.         mac.emb = &mac.mb[0];
  140.         mac.ppb = NULL;
  141. }
  142.  
  143.  
  144. /*
  145.  *      get character from input file or push back buffer
  146.  */
  147.  
  148. ngetc(infp)
  149. FILE *infp;
  150. {
  151.         int c;
  152.  
  153.         if (mac.ppb >= &mac.pbb[0]) {
  154.                 c = *mac.ppb--;
  155.         }
  156.         else {
  157.                 c = getc(infp);
  158.         }
  159.         return(c);
  160. }
  161.  
  162.  
  163.  
  164. /*
  165.  *      process input files from command line
  166.  */
  167.  
  168. profile()
  169. {
  170.         char ibuf[MAXLINE];
  171.  
  172.         for (dc.flevel=0; dc.flevel>=0; --dc.flevel) {
  173.                 while (getlin(ibuf,sofile[dc.flevel]) != EOF) {
  174.                         if (ibuf[0] == dc.cmdchr) comand(ibuf);
  175.                         else text(ibuf);
  176.                 }
  177.                 if (dc.flevel > 0) fclose(sofile[dc.flevel]);
  178.         }
  179.         if (pg.lineno > 0) space(HUGE);
  180. }
  181.  
  182.  
  183.  
  184. /*
  185.  *      process switch values from command line
  186.  */
  187.  
  188. pswitch(p,q)
  189. char *p;
  190. int *q;
  191. {
  192.         int swgood;
  193.  
  194.         swgood = TRUE;
  195.         if (*p == '-') {
  196.                 p++;
  197.                 switch (tolower(*p)) {
  198.                 case 'b':
  199.                         dc.bsflg = TRUE;
  200.                         break;
  201.                 case 'm':
  202.                         p++;
  203.                         if ((sofile[0] = fopen(p,"r")) == NULL) {
  204.                                 printf("***nro: unable to open file %s\n",p);
  205.                                 exit(-1);
  206.                         }
  207.                         profile();
  208.                         fclose(sofile[0]);
  209.                         break;
  210.                 case 'p':
  211.                         p++;
  212.                         set(&pg.offset,ctod(p),'1',0,0,HUGE);
  213.                         break;
  214.                 case 'u':
  215.                         dc.buflg = TRUE;
  216.                         break;
  217.                 case 'v':
  218.                         printf("NRO version 1.01 JRD 11/25/90\n");
  219.                         *q = TRUE;
  220.                         break;
  221.                 case '0':
  222.                 case '1':
  223.                 case '2':
  224.                 case '3':
  225.                 case '4':
  226.                 case '5':
  227.                 case '6':
  228.                 case '7':
  229.                 case '8':
  230.                 case '9':
  231.                         pg.lastpg = ctod(p);
  232.                         break;
  233.                 default:
  234.                         swgood = FALSE;
  235.                         break;
  236.                 }
  237.         }
  238.         else if (*p == '+') {
  239.                 p++;
  240.                 pg.frstpg = ctod(p);
  241.         }
  242.         else {
  243.                 swgood = FALSE;
  244.         }
  245.         if (swgood == FALSE) {
  246.                 printf("nro: illegal switch %s\n",p);
  247.                 return(ERR);
  248.         }
  249.         return(OK);
  250. }
  251.